﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace pcsse.MasterPages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        //protected db_news db = db_news.getDBhandle();
        protected DataTable dt;
        protected DataTable dl_dt;
        const int Page_size = 20;//每页显示的新闻数目
        protected int Page_num;
        private void DDL_Data_Bind()
        {
            dl_dt = db_news.getDBhandle().GetDataTableBySQL("SELECT * FROM T_CATEGORY");
            DropDownList1.DataSource = dl_dt;
            DropDownList1.DataValueField = dl_dt.Columns[0].ColumnName;
            DropDownList1.DataTextField = dl_dt.Columns[1].ColumnName;
            DropDownList1.DataBind();
        }
        //private void Data_Bind()
        //{
        //    //==========================================
        //    dt = db_news.getDBhandle().GetDataTableBySQL("SELECT * FROM T_NEWS,T_CATEGORY WHERE T_NEWS.CATEGORY_ID=T_CATEGORY.CATEGORY_ID");
        //    //DataList1.DataSource = dt;
        //    //DataList1.DataBind();
        //    //==========================================
        //    int curpage = Convert.ToInt32(lbPage.Text); 
        //    PagedDataSource ps = new PagedDataSource();
        //    ps.DataSource = dt.DefaultView;
        //    ps.AllowPaging = true; //是否可以分页 
        //    ps.PageSize = Page_size; //显示的数量 
        //    ps.CurrentPageIndex = curpage - 1; //取得当前页的页码
        //    //==========================================
        //    DataList1.DataSource = ps;
        //    DataList1.DataBind();
        //    if (!ps.IsFirstPage)
        //    {
        //        LbPrev.Enabled = true;
        //    }
        //    else
        //    {
        //        LbPrev.Enabled = false;
        //    }
        //    if (!ps.IsLastPage)
        //    {
        //        LbNext.Enabled = true;
        //    }
        //    else
        //    {
        //        LbNext.Enabled = false;
        //    }

        //}
        private void DL_PAGE(int page_index,DataTable in_dt)
        {
//            int cur_page_num;
  //          int curpage = Convert.ToInt32(lbPage.Text);
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = in_dt.DefaultView;
            ps.AllowPaging = true; //是否可以分页 
            ps.PageSize = Page_size; //显示的数量 
            ps.CurrentPageIndex = page_index; //取得当前页的页码

            LbPageCount.Text = "共" + ps.PageCount.ToString() + "页";
            //==========================================
            DataList1.DataSource = ps;
            DataList1.DataBind();
            if (!ps.IsFirstPage)
            {
                LbPrev.Enabled = true;
            }
            else
            {
                LbPrev.Enabled = false;
            }
            if (!ps.IsLastPage)
            {
                LbNext.Enabled = true;
            }
            else
            {
                LbNext.Enabled = false;
            }

        }
        private void DL_SelectByCategory(string CategoryID)
        {
            dt = db_news.getDBhandle().GetNewsByCategoryID(CategoryID);
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["page"] == null)
            {
                Page_num = 0;
            }
            else
            {
                Page_num = int.Parse(Request.QueryString["page"]);
            }
          

                lbPage.Text = "第"+(Page_num + 1).ToString()+"页";

            if (Request.QueryString["cateid"] == null)
            {
                Response.Redirect("../default.aspx");
                //DL_PAGE(Page_num, db_news.getDBhandle().GetNewsByCategoryID(DropDownList1.SelectedValue));
            }
            else
            {
                DL_PAGE(Page_num, db_news.getDBhandle().GetNewsByCategoryID(Request.QueryString["cateid"]));
                // DropDownList1.SelectedValue = Request.QueryString["cateid"];
                lb_title.Text = db_news.getDBhandle().GetCategoryById(int.Parse(Request.QueryString["cateid"]));
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void HyperLink1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("news_explore.aspx?cateid=" + DropDownList1.SelectedValue);
        }

        protected void Prev_Click(object sender, EventArgs e)
        {
            string urlstr = "news_explore.aspx?page=" + (Page_num - 1).ToString();
            if (Request.QueryString["cateid"] != null)
            {
                urlstr = urlstr + "&cateid=" + Request.QueryString["cateid"];
            }
            Response.Redirect(urlstr);
        }

        protected void Next_Click(object sender, EventArgs e)
        {
            string urlstr = "news_explore.aspx?page=" + (Page_num + 1).ToString();
            if (Request.QueryString["cateid"] != null)
            {
                urlstr = urlstr + "&cateid=" + Request.QueryString["cateid"];
            }
            Response.Redirect(urlstr);
        }



    }
}
